﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HGZP.BLL;
using HGZP.Model;
using HGZP.PLL;
using System.Data;

namespace SZCB.admin.Contry
{
    public partial class AddCity : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                BindFoodClass();
                BindFoodSubClass();
            }

        }

        private void BindFoodClass()
        {
            CCountryBLL myCCountryBLL = new CCountryBLL();
            ddlContentClass.DataSource = myCCountryBLL.F02_SelectHaveWhere("");
            ddlContentClass.DataTextField = myCCountryBLL.sName;
            ddlContentClass.DataValueField = myCCountryBLL.sGuid;
            ddlContentClass.DataBind();
            ListItem listItem = new ListItem();
            listItem.Text = "----请选择国家----";
            listItem.Value = "0";
            ddlContentClass.Items.Add(listItem);
            ddlContentClass.SelectedValue = "0";
        }

        private void BindFoodSubClass()
        {
            CProvinceBLL myCProvinceBLL = new CProvinceBLL();
            DropDownList1.DataSource = myCProvinceBLL.F02_SelectHaveWhere("sCountryGuid ='" + ddlContentClass.SelectedValue + "'");
            DropDownList1.DataTextField = myCProvinceBLL.sName;
            DropDownList1.DataValueField = myCProvinceBLL.sGuid;
            DropDownList1.DataBind();
            ListItem listItem = new ListItem();
            listItem.Text = "----请选择分类----";
            listItem.Value = "0";
            DropDownList1.Items.Add(listItem);
            DropDownList1.SelectedValue = "0";
        }

        protected void ddlContentClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlContentClass.SelectedValue == "0")
                return;
            BindFoodSubClass();
        }

        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            CCityBLL myCCityBLL = new CCityBLL();
            DataTable dt = myCCityBLL.F02_SelectHaveWhere("sName = '" + txtName.Text + "'");
            if (dt.Rows.Count > 0)
            {
                string strErrmessage = "该地市已添加";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;
            }
            CCity myCCity = new CCity();
            myCCity.sGuid = MyFunction.GetNewGuid();
            myCCity.sName = txtName.Text;
            myCCity.sCountryGuid = ddlContentClass.SelectedValue;
            myCCity.sProvinceGuid = DropDownList1.SelectedValue;
            if (!myCCityBLL.F_Insert(myCCity))
            {
                string strErrmessage = "添加地市失败";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;
            }

        }

    }
}